/*
 * Copyright 2014 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package it.gmariotti.cardslib.demo.ui.widget;

import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.ScrollView;
import ohos.agp.components.element.Element;
import ohos.agp.render.Canvas;
import ohos.agp.render.Path;
import ohos.agp.utils.Rect;
import ohos.app.Context;

public class ScrimInsetsScrollView extends ScrollView implements Component.DrawTask {
    private Element mInsetForeground;

    private Rect mInsets;
    private Rect mTempRect = new Rect();
    private OnInsetsCallback mOnInsetsCallback;

    public ScrimInsetsScrollView(Context context) {
        super(context);
        init(context, null, "0");
    }

    public ScrimInsetsScrollView(Context context, AttrSet attrs) {
        super(context, attrs);
        init(context, attrs, "0");
    }

    public ScrimInsetsScrollView(Context context, AttrSet attrs, String defStyle) {
        super(context, attrs, defStyle);
        init(context, attrs, defStyle);
    }

    private void init(Context context, AttrSet attrs, String defStyle) {
        if (attrs.getAttr("insetForeground").isPresent()) {
            mInsetForeground = attrs.getAttr("insetForeground").get().getElement();
        }

        addDrawTask(this);

        //todo no such method
        //        setWillNotDraw(true);
    }
    //todo no such method
//    @Override
//    protected boolean fitSystemWindows(Rect insets) {
//        mInsets = new Rect(insets);
//        setWillNotDraw(mInsetForeground == null);
//        ViewCompat.postInvalidateOnAnimation(this);
//        if (mOnInsetsCallback != null) {
//            mOnInsetsCallback.onInsetsChanged(insets);
//        }
//        return true; // consume insets
//    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        int width = getWidth();
        int height = getHeight();
        if (mInsets != null && mInsetForeground != null) {
            int sc = canvas.save();
            canvas.translate(getScrollValue(AXIS_X), getScrollValue(AXIS_Y));

            // Top
            mTempRect.set(0, 0, width, mInsets.top);
            mInsetForeground.setBounds(mTempRect);
            mInsetForeground.drawToCanvas(canvas);

            // Bottom
            mTempRect.set(0, height - mInsets.bottom, width, height);
            mInsetForeground.setBounds(mTempRect);
            mInsetForeground.drawToCanvas(canvas);

            // Left
            mTempRect.set(0, mInsets.top, mInsets.left, height - mInsets.bottom);
            mInsetForeground.setBounds(mTempRect);
            mInsetForeground.drawToCanvas(canvas);

            // Right
            mTempRect.set(width - mInsets.right, mInsets.top, width, height - mInsets.bottom);
            mInsetForeground.setBounds(mTempRect);
            mInsetForeground.drawToCanvas(canvas);

            canvas.restoreToCount(sc);
        }
    }

    //todo no such method
//        @Override
//        protected void onAttachedToWindow () {
//            super.onAttachedToWindow();
//            if (mInsetForeground != null) {
//                mInsetForeground.setCallback(this);
//            }
//        }


    //todo no such method
//        @Override
//        protected void onDetachedFromWindow () {
//            super.onDetachedFromWindow();
//            if (mInsetForeground != null) {
//                mInsetForeground.setCallback(null);
//            }
//        }

    /**
     * Allows the calling container to specify a callback for custom processing when insets change (i.e. when
     * {@link #(Rect)} is called. This is useful for setting padding on UI elements based on
     * UI chrome insets (e.g. a Google Map or a ListView). When using with ListView or GridView, remember to set
     * clipToPadding to false.
     * @param onInsetsCallback onInsetsCallback
     */
    public void setOnInsetsCallback(OnInsetsCallback onInsetsCallback) {
        mOnInsetsCallback = onInsetsCallback;
    }


    public static interface OnInsetsCallback {
        public void onInsetsChanged(Rect insets);
    }
}